package com.xhd.db;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.xBaseObj;
import com.xhd.config.BizObjClass;
import com.xhd.exception.DatabaseException;
import com.xhd.server.ServerConfig;

public class BizObjStore {
	private xBaseObj jm114 = null;
	  private Map jm505 = null;
	  private BizObjClass jm506;
	  private String jm507;
	  private int jm508;
	  private Field[] jm509;
	  private BizObjFields jm510;
	  private DBControl jm511;
	  private DaoValue jm512 = DaoValue.getInstance();
	  private static final Log jm9;
	  private static final BizObjStoreListener jm513;
	  static Class jm10;

	  static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	      
	      try
	      {
	        BizObjStore.jm10 = Class.forName("com.xhd.db.BizObjStore");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	    jm513 = BizObjStoreListener.getInstance();
	  }

	  public BizObjStore(xBaseObj paramxBaseObj)
	    throws DatabaseException
	  {
	    if (paramxBaseObj == null)
	      throw new DatabaseException(30041);
	    this.jm506 = BizObjClass.getInstance(paramxBaseObj.getClass());
	    this.jm507 = this.jm506.getName();
	    this.jm509 = this.jm506.getFields();
	    this.jm114 = paramxBaseObj;
	  }

	  public void setDBControl(DBControl paramDBControl, BizObjFields paramBizObjFields)
	    throws DatabaseException
	  {
	    if ((paramDBControl == null) || (paramBizObjFields == null))
	      throw new DatabaseException(30041);
	    this.jm511 = paramDBControl;
	    this.jm510 = paramBizObjFields;
	    this.jm508 = paramDBControl.getDBType();
	  }

	  public void setObject(xBaseObj paramxBaseObj)
	  {
	    if ((paramxBaseObj.getUserInfo() == null) && (this.jm505 != null))
	      paramxBaseObj.setUserBean(this.jm114.getUserInfo());
	    this.jm114 = paramxBaseObj;
	  }

	  public void setData(Map paramMap)
	  {
	    this.jm505 = paramMap;
	  }

	  public void setData(Object paramObject)
	  {
	    if (paramObject instanceof Map)
	    {
	      this.jm505 = ((Map)paramObject);
	    }
	    else
	    {
	      xBaseObj localxBaseObj = (xBaseObj)paramObject;
	      if ((localxBaseObj.getUserInfo() == null) && (this.jm505 != null))
	        localxBaseObj.setUserBean(this.jm114.getUserInfo());
	      this.jm114 = localxBaseObj;
	    }
	  }

	  public String getTalbeName()
	  {
	    return this.jm507;
	  }

	  public int getDBType()
	  {
	    return this.jm508;
	  }

	  protected String getObjName(Class paramClass)
	  {
	    String str = paramClass.getName();
	    int i = str.lastIndexOf(46);
	    if (i >= 0)
	      str = str.substring(i + 1);
	    return str.toLowerCase();
	  }

	  protected String getSQLValue(String paramString1, Field paramField, String paramString2)
	    throws DatabaseException
	  {
	    if (this.jm505 != null)
	      return this.jm512.getSQLValue(paramString1, paramString2, this.jm505, this.jm508);
	    return this.jm512.getSQLValue(paramString1, paramField, this.jm114, this.jm508);
	  }

	  private void jm504(DBSQL paramDBSQL, Field paramField, String paramString)
	    throws DatabaseException
	  {
	    int i = this.jm510.getJdbcType(paramString);
	    if (this.jm505 != null)
	      paramDBSQL.addParam(i, (String)this.jm505.get(paramString));
	    else
	      try
	      {
	        Object localObject = paramField.get(this.jm114);
	        if ((localObject == null) && (ServerConfig.BigDecimalSetZeroIfNull) && (paramField.getType().getName().endsWith("BigDecimal")))
	          localObject = new BigDecimal(0.0D);
	        paramDBSQL.addParam(i, localObject);
	      }
	      catch (Exception localException)
	      {
	        jm9.error(localException);
	        throw new DatabaseException(30084, paramString, localException.getMessage());
	      }
	  }

	  public int insert()
	    throws DatabaseException
	  {
	    jm513.onInsert(this.jm114, this.jm505);
	    DBSQL localDBSQL = new DBSQL();
	    StringBuffer localStringBuffer = new StringBuffer(256);
	    localDBSQL.append("INSERT INTO ");
	    localDBSQL.append(this.jm507);
	    localDBSQL.append("(");
	    localStringBuffer.append(") VALUES(");
	    if ((this.jm509 == null) || (this.jm509.length < 1))
	      return 0;
	    if ((this.jm510 == null) || (this.jm510.size() < 1))
	      return 0;
	    int i = 1;
	    for (int j = 0; j < this.jm509.length; ++j)
	    {
	      String str = this.jm509[j].getName().toLowerCase();
	      if (!(this.jm510.hasField(str)))
	        continue;
	      if (i == 0)
	      {
	        localDBSQL.append(",");
	        localStringBuffer.append(',');
	      }
	      else
	      {
	        i = 0;
	      }
	      localDBSQL.append(str);
	      localStringBuffer.append('?');
	      jm504(localDBSQL, this.jm509[j], str);
	    }
	    if (i != 0)
	      return 0;
	    localStringBuffer.append(')');
	    localDBSQL.append(localStringBuffer.toString());
	   int  j = this.jm511.execute(localDBSQL);
	    if (this.jm114.getDataLang() != null)
	      this.jm114.getDataLang().onInsert(this.jm511, this.jm505, this.jm510);
	    return j;
	  }

	  public int update(DBWhereExp paramDBWhereExp, List paramList1, List paramList2)
	    throws DatabaseException
	  {
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("UPDATE ");
	    localDBSQL.append(this.jm507);
	    localDBSQL.append(" SET ");
	    if ((this.jm509 == null) || (this.jm509.length < 1))
	      return 0;
	    if ((this.jm510 == null) || (this.jm510.size() < 1))
	      return 0;
	    int i = 0;
	    if (paramDBWhereExp == null)
	    {
	      i = 1;
	      BizObjPK   localObject = BizObjPK.getDBObjPK(this.jm114);
	      paramDBWhereExp = ((BizObjPK)localObject).getWhereExp(this.jm114);
	      if (paramList1 == null)
	        paramList1 = new Vector(1);
	      paramList1.addAll(((BizObjPK)localObject).getPKFields());
	    }
	    Object localObject = this.jm510.cloneInstance();
	    if (paramList1 != null)
	    {
	      ((BizObjFields)localObject).remove(paramList1);
	      if (((BizObjFields)localObject).size() < 1)
	        return 0;
	    }
	    if (paramList2 != null)
	    {
	      localObject = ((BizObjFields)localObject).subFields(paramList2);
	      if (localObject == null)
	        return 0;
	    }
	    if ((i == 0) || (paramList2 == null))
	    {
	      DBWhereExp localDBWhereExp = null;
	      if (paramList2 == null)
	        localDBWhereExp = jm513.onUpdate(this.jm114, (BizObjFields)localObject);
	      else
	        localDBWhereExp = jm513.onUpdate(this.jm114, null);
	      if ((i != 0) && (localDBWhereExp != null))
	      {
	        localDBWhereExp.andSubExp(paramDBWhereExp);
	        paramDBWhereExp = localDBWhereExp;
	      }
	    }
	    if ((i != 0) && (this.jm114.getDataLang() != null))
	      this.jm114.getDataLang().onUpdate(this.jm511, paramDBWhereExp, (BizObjFields)localObject, this.jm510);
	    if (((BizObjFields)localObject).size() < 1)
	      return 0;
	    int j = this.jm511.getDBType();
	    int k = 1;
	    for (int l = 0; l < this.jm509.length; ++l)
	    {
	      String str = this.jm509[l].getName().toLowerCase();
	      if (!(((BizObjFields)localObject).hasField(str)))
	        continue;
	      if (k == 0)
	        localDBSQL.append(",");
	      else
	        k = 0;
	      localDBSQL.append(str);
	      localDBSQL.append("=?");
	      jm504(localDBSQL, this.jm509[l], str);
	    }
	    if ((paramDBWhereExp != null) && (!(paramDBWhereExp.isNull())))
	    {
	      localDBSQL.append(" WHERE ");
	      localDBSQL.append(j, paramDBWhereExp);
	    }
	    return this.jm511.execute(localDBSQL);
	  }

	  public boolean select(DBWhereExp paramDBWhereExp)
	    throws DatabaseException
	  {
	    if (paramDBWhereExp == null)
	    {
	      paramDBWhereExp = BizObjPK.getDBObjPK(this.jm114).getWhereExp(this.jm114);
	    }
	    else
	    {
	    	DBWhereExp  localObject1 = jm513.onSelect(this.jm114);
	      if (localObject1 != null)
	      {
	        ((DBWhereExp)localObject1).andSubExp(paramDBWhereExp);
	        paramDBWhereExp = (DBWhereExp)localObject1;
	      }
	    }
	    Object localObject1 = new DBSQL();
	    ((DBSQL)localObject1).append("SELECT * FROM ");
	    ((DBSQL)localObject1).append(this.jm507);
	    if ((paramDBWhereExp != null) && (!(paramDBWhereExp.isNull())))
	    {
	      ((DBSQL)localObject1).append(" WHERE ");
	      ((DBSQL)localObject1).append(this.jm508, paramDBWhereExp);
	    }
	    PreparedStatement localPreparedStatement = null;
	    ResultSet localResultSet = null;
	    String str1 = ((DBSQL)localObject1).toString();
	    long l = (ServerConfig.IsLogSQLCost) ? System.currentTimeMillis() : 0L;
	    try
	    {
	      localPreparedStatement = this.jm511.getConn().prepareStatement(((DBSQL)localObject1).getSQL());
	      ((DBSQL)localObject1).toPreparedStatement(localPreparedStatement);
	      localResultSet = localPreparedStatement.executeQuery();
	      if ((localResultSet == null) || (!(localResultSet.next())))
	        return false;
	      ResultSetMetaData localResultSetMetaData = localResultSet.getMetaData();
	      int i = localResultSetMetaData.getColumnCount();
	      for (int j = 1; j <= i; ++j)
	      {
	        String str2 = localResultSetMetaData.getColumnName(j).toLowerCase();
	        Field localField = this.jm506.getField(str2);
	        if (localField == null)
	          continue;
	        String str3 = getObjName(localField.getType());
	        Object localObject2;
	        if (str3.equals("string"))
	        {
	          localObject2 = localResultSet.getString(j);
	          if (localObject2 != null)
	            localObject2 = DBUtil.filterStringFromDB((String)localObject2);
	          else
	            localObject2 = "";
	          localField.set(this.jm114, localObject2);
	          localObject2 = null;
	        }
	        else if (str3.equals("int"))
	        {
	          localField.setInt(this.jm114, localResultSet.getInt(j));
	        }
	        else if (str3.equals("bigdecimal"))
	        {
	          localObject2 = (BigDecimal)localResultSet.getObject(j);
	          if (localObject2 == null)
	            localObject2 = new BigDecimal(0.0D);
	          localField.set(this.jm114, localObject2);
	        }
	        else if (str3.equals("date"))
	        {
	          localObject2 = (Date)localResultSet.getObject(j);
	          if (localObject2 == null)
	            continue;
	          localField.set(this.jm114, localObject2);
	        }
	        else
	        {
	          throw new DatabaseException(30085, str2, str3);
	        }
	      }
	      localResultSet.close();
	      localResultSet = null;
	      if (this.jm114.getDataLang() != null)
	        this.jm114.getDataLang().onSelect(this.jm511, paramDBWhereExp);
	      return true;
	    }
	    catch (DatabaseException localDatabaseException)
	    {
	      throw localDatabaseException;
	    }
	    catch (SQLException localSQLException1)
	    {
	      jm9.error(localObject1, localSQLException1);
	      throw new DatabaseException(30031, str1, localSQLException1);
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localObject1, localException);
	      throw new DatabaseException(localException.getMessage());
	    }
	    finally
	    {
	      try
	      {
	        if (localResultSet != null)
	          localResultSet.close();
	        if (localPreparedStatement != null)
	          localPreparedStatement.close();
	        localPreparedStatement = null;
	      }
	      catch (SQLException localSQLException2)
	      {
	        jm9.error(localSQLException2);
	        throw new DatabaseException(30023);
	      }
	      this.jm511.log(str1, l);
	    }
	  }

	  public int delete(DBWhereExp paramDBWhereExp)
	    throws DatabaseException
	  {
	    DBSQL localDBSQL = new DBSQL();
	    int i = 0;
	    if (paramDBWhereExp == null)
	    {
	      i = 1;
	      paramDBWhereExp = BizObjPK.getDBObjPK(this.jm114).getWhereExp(this.jm114);
	    }
	    else
	    {
	      DBWhereExp localDBWhereExp = jm513.onDelete(this.jm114);
	      if (localDBWhereExp != null)
	      {
	        localDBWhereExp.andSubExp(paramDBWhereExp);
	        paramDBWhereExp = localDBWhereExp;
	      }
	    }
	    localDBSQL.append("DELETE FROM ");
	    localDBSQL.append(this.jm507);
	    if ((paramDBWhereExp != null) && (!(paramDBWhereExp.isNull())))
	    {
	      localDBSQL.append(" WHERE ");
	      localDBSQL.append(this.jm508, paramDBWhereExp);
	    }
	    int j = this.jm511.execute(localDBSQL);
	    if ((i != 0) && (this.jm114.getDataLang() != null))
	      this.jm114.getDataLang().onDelete(this.jm511, paramDBWhereExp);
	    return j;
	  }

	  public int genId(DBControl paramDBControl, String paramString)
	    throws DatabaseException
	  {
	    DBConn localDBConn = null;
	    try
	    {
	      if (paramString == null)
	      {
	        BizObjPK localBizObjPK = BizObjPK.getDBObjPK(this.jm114);
	        paramString = localBizObjPK.getPKField();
	      }
	      localDBConn = paramDBControl.getConn();
	      localDBConn.setTransation(true);
	      int i = paramDBControl.IDGenerator(this.jm511, this.jm507, paramString, false);
	      localDBConn.commit();
	      int j = i;
	      return j;
	    }
	    finally
	    {
	      if (localDBConn != null)
	        localDBConn.close();
	    }
	  }

	  public int genBuildInId(DBControl paramDBControl, String paramString)
	    throws DatabaseException
	  {
	    DBConn localDBConn = null;
	    try
	    {
	      if (paramString == null)
	      {
	        BizObjPK localBizObjPK = BizObjPK.getDBObjPK(this.jm114);
	        paramString = localBizObjPK.getPKField();
	      }
	      localDBConn = paramDBControl.getConn();
	      localDBConn.setTransation(true);
	      int i = paramDBControl.IDGenerator(this.jm511, this.jm507, paramString, true);
	      localDBConn.commit();
	      int j = i;
	      return j;
	    }
	    finally
	    {
	      if (localDBConn != null)
	        localDBConn.close();
	    }
	  }

	  public Vector search(String paramString, DBWhereExp paramDBWhereExp, int paramInt)
	    throws DatabaseException
	  {
	    DBWhereExp localDBWhereExp = jm513.onSearch(this.jm114);
	    if (localDBWhereExp != null)
	    {
	      localDBWhereExp.andSubExp(paramDBWhereExp);
	      paramDBWhereExp = localDBWhereExp;
	    }
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("SELECT ");
	    if (paramString == null)
	      localDBSQL.append("*");
	    else
	      localDBSQL.append(paramString);
	    localDBSQL.append(" FROM ");
	    localDBSQL.append(this.jm507);
	    if ((paramDBWhereExp != null) && (!(paramDBWhereExp.isNull())))
	    {
	      localDBSQL.append(" WHERE ");
	      localDBSQL.append(getDBType(), paramDBWhereExp);
	    }
	    return this.jm511.multiRowSelect(localDBSQL, paramInt, 0);
	  }

	  public boolean checkExists(DBWhereExp paramDBWhereExp)
	    throws DatabaseException
	  {
	    DBWhereExp localDBWhereExp = jm513.onSearch(this.jm114);
	    if (localDBWhereExp != null)
	    {
	      localDBWhereExp.andSubExp(paramDBWhereExp);
	      paramDBWhereExp = localDBWhereExp;
	    }
	    DBSQL localDBSQL = new DBSQL();
	    if (1 == this.jm511.getDBType())
	      localDBSQL.append("SELECT 1 FROM dual");
	    else
	      localDBSQL.append("SELECT 1");
	    localDBSQL.append(" WHERE exists(SELECT * FROM ");
	    localDBSQL.append(this.jm507);
	    if (paramDBWhereExp != null)
	    {
	      localDBSQL.append(" WHERE ");
	      localDBSQL.append(this.jm511.getDBType(), paramDBWhereExp);
	    }
	    localDBSQL.append(")");
	    return (this.jm511.intSelect(localDBSQL) > 0);
	  }

	  public int getCount(DBWhereExp paramDBWhereExp)
	    throws DatabaseException
	  {
	    DBWhereExp localDBWhereExp = jm513.onSearch(this.jm114);
	    if (localDBWhereExp != null)
	    {
	      localDBWhereExp.andSubExp(paramDBWhereExp);
	      paramDBWhereExp = localDBWhereExp;
	    }
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("SELECT count(*)");
	    localDBSQL.append(" FROM ");
	    localDBSQL.append(this.jm507);
	    if (paramDBWhereExp != null)
	    {
	      localDBSQL.append(" WHERE ");
	      localDBSQL.append(this.jm511.getDBType(), paramDBWhereExp);
	    }
	    return this.jm511.intSelect(localDBSQL);
	  }
}
